/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn;

import cn.webrx.mapper.BookMapper;
import cn.webrx.mapper.StudentMapper;
import cn.webrx.mapper.TeacherMapper;
import cn.webrx.mapper.UserMapper;
import cn.webrx.mybatis.inerceptor.MyPager;
import cn.webrx.pojo.Book;
import cn.webrx.pojo.Teacher;
import cn.webrx.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Project: mybatis2021 - Demo
 * <p>Powered by webrx On 2021-10-23 17:48:53
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@Slf4j
public class Demo {
    StudentMapper sm = null;
    BookMapper bm = null;
    SqlSession ss = null;
    UserMapper um = null;
    TeacherMapper tm = null;

    @Before
    public void init() throws IOException {
        var is = Resources.getResourceAsStream("mybatis-conf.xml");
        var sf = new SqlSessionFactoryBuilder().build(is);

        var cfg = sf.getConfiguration();
        
        var mh = new MapperHelper();
        mh.registerMapper(Mapper.class);
        mh.processConfiguration(cfg);


        ss = sf.openSession(true);
        sm = ss.getMapper(StudentMapper.class);
        um = ss.getMapper(UserMapper.class);
        bm = ss.getMapper(BookMapper.class);
        tm = ss.getMapper(TeacherMapper.class);
    }

    @Test
    public void t6() {
        //tm.insert(new Teacher("李强"));

        //tm.deleteByPrimaryKey(1);
        var t = new Teacher();
        t.setId(2);
        t = tm.selectOne(t);

        log.info("{}", t);

        t.setName("张五丰");

        tm.updateByPrimaryKey(t);

        log.info("{}", tm.selectAll());

        log.info("{}", tm.dbs());

    }


    @Test
    public void t5() {
        MyPager.startPage(1, 5);
        log.info("{}", bm.myByPage());
    }


    @Test
    public void t4() {
        var list = bm.pp();

        //PageHelper.startPage(1, 3);
        //var list = bm.pp();
        //log.info("{}", list.get(1));
        //log.info("{}", list.get(2));
        //log.info("{}", list.get(0));
        //log.info("{}", ist.size());

        //PageHelper.startPage(1, 5);
        //排序分页
        //PageHelper.startPage(1, 5, "id desc");
        //true参数代表统计总记录数
        //PageHelper.startPage(1, 5, true);
        PageHelper.startPage(100, 3, true, true, false);

        PageInfo pi = PageInfo.of(bm.pp());
        log.info("{}", pi.getList().get(0));
        //log.info("{}", pi.getList().get(1));
        //log.info("{}", pi.getList().get(2));

        log.info("[第{}页，共{}页][每页{}条，共{}条]", pi.getPageNum(), pi.getPages(), pi.getPageSize(), pi.getTotal());
    }

    @Test
    public void t3() {
        //int num = bm.count("price>50");
        //log.info("{}", num);

        int pagesize = 7;
        int currpage = 1;
        List<Book> list = bm.page(currpage * pagesize - pagesize, pagesize);
        //List<Book> list = bm.page();
        log.info("{}--{}条记录", list, list.size());
    }


    @Test
    public void t2() throws IOException {
        User u = new User();
        u.setName("wangwu");
        u.setStudent(true);
        u.setBirth(LocalDate.of(1985, 10, 22));
        //u.setRegtime(LocalDateTime.now());
        u.setRegtime(new Date());
        File file;
        FileInputStream fis = new FileInputStream("c:/ok.jpg");
        byte[] buf = fis.readAllBytes();
        u.setPhoto(buf);
        um.save(u);

    }

    @Test
    public void t1() {
        //log.info("{}", sm.tbs());

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "xxxxxx");
        map.put("author", "aa");
        map.put("price", 33);
        //sm.add(map);
        sm.save(map);

        System.out.println(map.get("id"));
    }
}
